package leetcode_1_20;

import java.util.ArrayList;
import java.util.List;

public class letterCombinations_17 {
    public static List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return ans;
        }char []arr=digits.toCharArray();
        List<char[]>allChars=new ArrayList<>();
        for (char c:arr) {
            switch (c){
                case '2':allChars.add(new char[]{'a','b','c'});break;
                case '3':allChars.add(new char[]{'d','e','f'});break;
                case '4':allChars.add(new char[]{'g','h','i'});break;
                case '5':allChars.add(new char[]{'j','k','l'});break;
                case '6':allChars.add(new char[]{'m','n','o'});break;
                case '7':allChars.add(new char[]{'p','q','r','s'});break;
                case '8':allChars.add(new char[]{'t','u','v'});break;
                case '9':allChars.add(new char[]{'w','x','y','z'});break;
            }
        }
        function(allChars,0);
        return ans;
    }
    public static List<String>ans=new ArrayList<>();
    public static StringBuilder b=new StringBuilder();             // StringBuilder的方法易于操作
    public static void function(List<char[]> chars,int index){
        if(index == chars.size()){
            ans.add(b.toString());
            return;
        }
        for(int i=0;i<chars.get(index).length;i++){
            b.append(chars.get(index)[i]);
            function(chars,index+1);
            //关键一步，上一层递归结束，需要遍历下一个字母，
            //此时删除公共字符串上一次尾部添加的字符，添加新的字符
            b.deleteCharAt(b.length()-1);
        }
    }
}
